*Estimate the effect of PPE and WB/WC statutues on compliance with OSHA regulations 


global project "C:/Users/msj22/Dropbox/research/wrongful_discharge/replication_May_2022/"
global replication_rawdata "${project}data/"
global replication_cleandata "${project}intermediate_dtas/"
	


	u ${replication_cleandata}osha_inspection_cleaned.dta, clear

	global sample programmed==1 & insp_scope!="D" & ( (open_year>=1979 & state_run_office!=1) | (open_year>=1992 & state_run_office==1) ) & open_year<=2002 & adv_notice!="Y" & records_only_insp !=1
	
	global fes_base state_code sic1#open_year state_run_office#open_year 
	
	estimates clear 
	
	**************************
	*Quick program for saving regression results
	*************************
	capture program drop estoutme 
	program define estoutme
	syntax , [depvar(str)]
	
		estadd local basicFE "Yes"
		estadd local sic1_year "Yes" 			
		estadd local federal_year "Yes" 		
		if "`depvar'"=="asinh_gravity" {
			qui sum gravity if e(sample)  
		}
		else {
			qui sum `e(depvar)' if e(sample)  
		}
		estadd scalar meandv = r(mean)
	
	end 
	
	
	*One for all sectors, once removing construction 
		global sample_all 
		global sample_nocon "& (sic2!=17 & sic2!=16 & sic2!=15 ) "
		
		local sectors all nocon 
		local depvars asinh_gravity 
		
		estimates clear 
		
		foreach sector in `sectors' {
		    
			foreach depvar in `depvars' {
				*First, quietly set the sample by running the most saturated model, which loses 2 observations 
					cap drop in_sample
					qui reghdfe `depvar' wdlap wb wc  wdlag wdlac ln_nr_in_estab union partial_insp if $sample   ${sample_`sector'}  , a($fes_base c.open_year#state_code region#year) cluster(state_code)
						gen in_sample = e(sample)==1

				*PPE
					reghdfe `depvar' wdlap  ln_nr_in_estab union partial_insp if $sample  & in_sample==1  ${sample_`sector'}, a($fes_base) cluster(state_code)
						estimates store `depvar'_ppe_`sector'
						estoutme 
						estadd local state_trend "No"
						estadd local region_year "No" 			
				
				*Everything 
					reghdfe `depvar' wdlap wb wc  wdlag wdlac ln_nr_in_estab union partial_insp if $sample & in_sample==1  ${sample_`sector'}  , a($fes_base) cluster(state_code)
						estimates store `depvar'_3_`sector'
						estoutme 
						estadd local state_trend "No"
						estadd local region_year "No" 			

				*Everything: + div-year 
					reghdfe `depvar' wdlap wb wc  wdlag wdlac ln_nr_in_estab union partial_insp if $sample & in_sample==1  ${sample_`sector'}  , a($fes_base region#year_quarter) cluster(state_code)
						estimates store `depvar'_3_`sector'_r
						estoutme 
						estadd local region_year "Yes" 			
						estadd local state_trend "No"

				*Everything: + state trends + div-year 
					reghdfe `depvar' wdlap wb wc  wdlag wdlac ln_nr_in_estab union partial_insp if $sample & in_sample==1  ${sample_`sector'}  , a($fes_base c.open_year#state_code region#year) cluster(state_code)
						estimates store `depvar'_3_`sector'_tr
						estoutme 
						estadd local state_trend "Yes"
						estadd local region_year "Yes" 			
			} // depvar 
		} // sample 
		

		
/*
		esttab *gravity*_ppe_all* *gravity*_3_all* using "${wrongful_discharge}/wdl_compliance_gravity_all_sectors.tex", ///
		booktabs fragment replace scalars("meandv Mean Dep Var (in levels)" "basicFE Sector-state and sector-year FE" "sic1_year Industry year FE" "region_year Division-year FE" "state_trend State trend"  ) ///
		se b(3) keep(wdlap wb wc wdlag wdlac ln_nr_in_estab union ) order(wdlap wb wc wdlag wdlac ln_nr_in_estab union )  star(* .1 ** .05 *** .01)   label nodepvar ///
		nonotes nomtitles  	
	
		esttab *gravity*_ppe_nocon* *gravity*_3_nocon* using "${wrongful_discharge}/wdl_compliance_gravity_no_construction.tex", ///
		booktabs fragment replace scalars("meandv Mean Dep Var (in levels)" "basicFE Sector-state and sector-year FE" "sic1_year Industry year FE" "region_year Division-year FE" "state_trend State trend"  ) ///
		se b(3) keep(wdlap wb wc wdlag wdlac ln_nr_in_estab union ) order(wdlap wb wc wdlag wdlac ln_nr_in_estab union )  star(* .1 ** .05 *** .01)   label nodepvar ///
		nonotes nomtitles  	
*/
	